package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.constant.AuthConstant;
import net.wanho.entity.AuthInfo;
import net.wanho.service.AuthInfoService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthController {
    private final AuthInfoService authInfoService;

    /**
     *  获取权限树
     */
    @GetMapping("/auth-tree")
    public AjaxResult getAuthTree() {
        return AjaxResult.success(authInfoService.getAuthTree());
    }

    /**
     *  权限名称校验
     */
    @GetMapping("/name-check")
    public AjaxResult nameCheck(String authName) {
        return AjaxResult.success(authInfoService.nameCheck(authName));
    }

    /**
     *  权限Url校验
     */
    @GetMapping("/url-check")
    public AjaxResult urlCheck(String authUrl) {
        return AjaxResult.success(authInfoService.urlCheck(authUrl));
    }

    /**
     *  权限代码校验
     */
    @GetMapping("/code-check")
    public AjaxResult codeCheck(String authCode) {
        return AjaxResult.success(authInfoService.codeCheck(authCode));
    }

    /**
     *  添加权限
     */
    @PostMapping("/auth-add")
    public AjaxResult authAdd(@RequestBody AuthInfo authInfo) {
        authInfoService.saveAuth(authInfo);
        return AjaxResult.success(null, "权限添加成功！");
    }

    /**
     *  启用权限
     */
    @PutMapping("/auth-enable/{id}")
    public AjaxResult authEnable(@PathVariable Integer id) {
        authInfoService.updateAuthStateById(id, AuthConstant.ENABLE_AUTH);
        return AjaxResult.success(null, "权限启用成功！");
    }

    /**
     *  禁用权限
     */
    @PutMapping("/auth-disable/{id}")
    public AjaxResult authDisable(@PathVariable Integer id) {
        authInfoService.updateAuthStateById(id, AuthConstant.DISABLE_AUTH);
        return AjaxResult.success(null, "权限禁用成功！");
    }

    /**
     *  修改权限
     */
    @PutMapping("/auth-update")
    public AjaxResult authUpdate(@RequestBody AuthInfo authInfo) {
        authInfoService.updateById(authInfo);
        return AjaxResult.success(null, "权限修改成功！");
    }

    /**
     *  删除权限
     */
    @DeleteMapping("/auth-delete/{id}")
    public AjaxResult authDelete(@PathVariable Integer id) {
        authInfoService.removeAuthById(id);
        return AjaxResult.success(null, "权限删除成功！");
    }
}
